﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WX.WMS.DataModel;
namespace WX.WMS.BLL
{
    public class UserBLL:BaseBLL
    {
        private BaseDAO DAO=null;
        public UserBLL()
        {
            DAO = new BaseDAO();
        }
        public bool IsLoginValidate(string UserName, string Password)
        {
           var users= DAO.T_SYS_USERs.Where(t => t.USER == UserName && t.PASSWORD == Password);
           if (users.Count() > 0) return true;
           else return false;
        }
        public T_SYS_USER GetUserByName(string Name)
        { 
            var users =DAO.T_SYS_USERs.Where(t => t.USER == Name);
            if (users.Count() > 0)
                return users.FirstOrDefault();
            else
                return null;
        }
        public T_SYS_USER GetUserByID(string ID)
        {
            var users = DAO.T_SYS_USERs.Where(t => t.ID.ToString() ==ID );
            if (users.Count() > 0)
                return users.FirstOrDefault();
            else
                return null;
        }
        public IEnumerable<T_SYS_USER> GetUserList()
        {
            return DAO.T_SYS_USERs.OrderBy(t=>t.NAME).ToList();
        }
        public int Add(T_SYS_USER user)
        {
            try
            {

                user.ID = DAO.GetGUID();
                DAO.T_SYS_USERs.InsertOnSubmit(user);
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        public int Update(T_SYS_USER user)
        {
            try
            {
                T_SYS_USER RawUser = DAO.T_SYS_USERs.SingleOrDefault(t=>t.ID.ToString()==user.ID.ToString());
                RawUser.NAME = user.NAME;
                RawUser.PASSWORD = user.PASSWORD;
                RawUser.T_SYS_ROLE = DAO.T_SYS_ROLEs.SingleOrDefault(t=>t.ID.ToString()==user.T_SYS_ROLE.ID.ToString());
                RawUser.STATUS = user.STATUS;
                DAO.SubmitChanges();
                return 1;

            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        public IEnumerable<T_SYS_USER> SearchUser(string name, string user)
        {
            return null;
        }
    }
}
